//
//  BaseCollectionView.swift
//  cabbage_swift
//
//  Created by dfsx6 on 2021/9/9.
//

import Foundation
import UIKit

//MARK:-
public protocol BaseCollectionViewDelegate: NSObjectProtocol {
    
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath, cell: UICollectionViewCell)

    func collectionViewDidScroll(_ scrollView: UIScrollView)
    func collectionViewWillBeginDragging(_ scrollView: UIScrollView)
}

//MARK:-
open class BaseCollectionView: UIView {
    
    open weak var delegate: BaseCollectionViewDelegate?
    public var protocols: [BaseCollectionSectionProtocol] = [] {
        didSet {
            protocols.forEach{ $0.registerForCollectionViewCell(self.collectionView) }
            protocols.forEach{ $0.registerForSupplementaryView(self.collectionView) }
        }
    }
    
    ///
    public lazy var collectionView: UICollectionView = {
        let layout = UICollectionViewFlowLayout.init()
        layout.minimumLineSpacing = 0.0
        layout.minimumInteritemSpacing = 0.0
        layout.sectionInset = .zero
        layout.estimatedItemSize = UICollectionViewFlowLayout.automaticSize

        let collectionView = UICollectionView.init(frame: .zero, collectionViewLayout: layout)
        collectionView.delegate = self
        collectionView.dataSource = self
        collectionView.backgroundColor = .clear
        collectionView.showsVerticalScrollIndicator = false
        collectionView.showsHorizontalScrollIndicator = false
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "UICollectionReusableView")
        collectionView.register(UICollectionReusableView.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionFooter, withReuseIdentifier: "UICollectionReusableView")

        return collectionView
    }()
        
    ///
    public override init(frame: CGRect) {
        super.init(frame: frame)
        
       // collectionView.frame = CGRect.init(origin: .zero, size: frame.size)
        addSubview(collectionView)
    }
    
    required public init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    open override func layoutSubviews() {
        super.layoutSubviews()
        
        collectionView.frame = bounds
    }
    
    deinit {
        print(#file, #function)
    }
}


//MARK:-
extension BaseCollectionView: UICollectionViewDataSource {
    
    public func numberOfSections(in collectionView: UICollectionView) -> Int {
        return protocols.count
    }
    
    public func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        let item = protocols[section]
        return item.numberOfItemsInSection(collectionView)
    }
    
    public func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = protocols[indexPath.section].collectionView(collectionView, cellForItemAt: indexPath)
        delegate?.collectionView(collectionView, cellForItemAt: indexPath, cell: cell)
        return cell
    }
    
    public func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
        let item = protocols[indexPath.section]
        return item.collectionView(collectionView, viewForSupplementaryElementOfKind: kind, at: indexPath)
    }

    
}

//MARK:-
extension BaseCollectionView: UICollectionViewDelegate {
    
    public func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let item = protocols[indexPath.section]
        item.collectionView(collectionView, didSelectItemAt: indexPath)
    }
}

//MARK:-
extension BaseCollectionView: UICollectionViewDelegateFlowLayout {

    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize {
        let item = protocols[indexPath.section]
        return item.collectionView(collectionView, layout: collectionViewLayout, sizeForItemAt: indexPath)
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForHeaderInSection section: Int) -> CGSize {
        let item = protocols[section]
        return item.collectionView(collectionView, layout: collectionViewLayout, referenceSizeForHeaderInSection: section)
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, referenceSizeForFooterInSection section: Int) -> CGSize {
        let item = protocols[section]
        return item.collectionView(collectionView, layout: collectionViewLayout, referenceSizeForFooterInSection: section)
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat {
        let item = protocols[section]
        return item.collectionView(collectionView, layout: collectionViewLayout, minimumLineSpacingForSectionAt: section)
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumInteritemSpacingForSectionAt section: Int) -> CGFloat {
        let item = protocols[section]
        return item.collectionView(collectionView, layout: collectionViewLayout, minimumInteritemSpacingForSectionAt: section)
    }
    
    public func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets {
        let inset = protocols[section].collectionView(collectionView, layout: collectionViewLayout, insetForSectionAt: section)
        return inset
    }
}

//MARK:-
extension BaseCollectionView: UIScrollViewDelegate {
    public func scrollViewDidScroll(_ scrollView: UIScrollView) {
        delegate?.collectionViewDidScroll(scrollView)
    }
    
    public func scrollViewWillBeginDragging(_ scrollView: UIScrollView) {
        delegate?.collectionViewWillBeginDragging(scrollView)
    }
}
